package com.sun.showcase.controller.app.mall;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.showcase.biz.basic.utils.FtpClientConstants;
import com.sun.showcase.biz.mall.service.GoodsQrcodeService;
import com.sun.showcase.biz.mall.service.GoodsService;
import com.sun.showcase.client.domain.mall.GoodsQrcode;
import com.sun.showcase.client.query.mall.GoodsQrcodeQuery;
import com.sun.showcase.client.query.mall.GoodsQuery;
import com.sun.showcase.conf.wx.miniapp.WxMaConfiguration;
import com.sun.showcase.conf.wx.miniapp.WxMaProperties;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.utils.FileUpload;
import com.sun.showcase.utils.SpringApplicationContextHolder;
import com.sun.showcase.utils.StringConstant;

import cn.binarywang.wx.miniapp.api.WxMaService;
@CrossOrigin
@Controller("appGoodsController")
@RequestMapping(value="/app/goods") 
public class GoodsController{
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private GoodsQrcodeService goodsQrcodeService;
	@Autowired
	private WxMaProperties properties;
	private Result result = new Result();

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/list")
	public Result list(GoodsQuery goodsQuery) {
		goodsQuery.setActiveFlag(StringConstant.ONE);
		DataGrid dg =  goodsService.datagrid(goodsQuery);
		result.setSuccess(true);
		result.setData(dg);
		return result;
	}
	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/get")
	public Result get(String id) {
		GoodsQuery goods = goodsService.get4App(id);
		result.setSuccess(true);
		result.setData(goods);
		return result;
	}
	@ResponseBody
	@RequestMapping(value="getQrCode")
	public Result getQrCode(String id,String fatherId) {
		Result result = new Result();
		
		GoodsQrcode mem = goodsQrcodeService.getByGoodsAndUserId(id, fatherId);
		if(mem != null && StringUtils.isNotBlank(mem.getQrcode())) {
			result.setSuccess(true);
			result.setData(mem.getQrcode());
			return result;
		}else {
			if(mem == null) {
				GoodsQrcodeQuery goodsQrcodeQuery = new GoodsQrcodeQuery();
				goodsQrcodeQuery.setGoodsId(id);
				goodsQrcodeQuery.setUserId(fatherId);
				mem = goodsQrcodeService.add(goodsQrcodeQuery);
			}
			final WxMaService wxService = WxMaConfiguration.getMaService(properties.getConfigs().get(0).getAppid());
			try {
				String param = mem.getId();
				File f;
				try {
					f = wxService.getQrcodeService().createWxaCodeUnlimit(param, "pages/product/board");
				} catch (Exception e) {
					f = wxService.getQrcodeService().createWxaCodeUnlimit(param, null);
				}
				FtpClientConstants ftpClientConstants=(FtpClientConstants)SpringApplicationContextHolder.getBean("ftpClientConstants");
				String imagePath = FileUpload.uploadLocal2("picture", f);
				String qrCode = ftpClientConstants.getPrefixUrl()+ imagePath;
				GoodsQrcodeQuery up = new GoodsQrcodeQuery();
				up.getSearchMap().put("id", param);
				up.setQrcode(qrCode);
				goodsQrcodeService.updatePart(up);
				result.setSuccess(true);
				result.setData(qrCode);
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
				result.setMsg("获取二维码失败，请稍后重试！");
				return result;
			}
		}
		return result;
		
	}
	@ResponseBody
	@RequestMapping(value="getQrCodeInfo")
	public Result getQrCodeInfo(String id) {
		Result result = new Result();
		GoodsQrcode mem = goodsQrcodeService.get(id);
		result.setData(mem);
		result.setSuccess(true);
		return result;
		
	}
}
